<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="0">
    <title></title>
    <script type="text/javascript" src="../../js/commonCss.js"></script>
    <link rel="stylesheet" type="text/css" href="../../styles/common.css" />
    <style>
        .modal-backdrop.in {
             opacity: .0;
        }
        .modal-dialog {
            margin: 0;
        }
        .form-control[disabled], .form-control[readonly], fieldset[disabled] .form-control {
            cursor: auto;
        }
        .modal-open .modal {
            overflow-x: hidden;
            overflow-y: hidden;
        }
    </style>
</head>
<body>

<div class="modal" tabindex="-1" role="dialog" id="modal2">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form id="form2">
                <div class="modal-header">
                    <button type="button" class="close hideModal" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">修改</h4>
                </div>
                <div class="modal-body">
                    <form class="form">
                        <div class="form-group fix">
                            <div class="input-group w400 mt10">
                                <div class="input-group-addon">账号&nbsp;&nbsp;&nbsp;</div>
                                <input type="text" class="form-control" name="loginCode" id="loginCode" maxlength="32" disabled>
                            </div>
                            <div class="input-group w400 mt10">
                                <div class="input-group-addon">用户名</div>
                                <input type="text" class="form-control" name="userName" id="userName" maxlength="32" disabled>
                                <div class="required-icon">*</div>
                            </div>
                            <div class="input-group w400 mt10 pwd">
                                <div class="input-group-addon">原密码</div>
                                <input type="password" class="form-control" id="oldPassword" maxlength="32" name="oldPassword">
                                <div class="required-icon">*</div>
                            </div>
                            <div class="input-group w400 mt10 pwd">
                                <div class="input-group-addon">新密码</div>
                                <input type="password" class="form-control" id="newPassword" maxlength="32" name="newPassword">
                                <div class="required-icon">*</div>
                            </div>
                            <div class="input-group w400 mt10 pwd">
                                <div class="input-group-addon">确认密码</div>
                                <input type="password" class="form-control" id="confirmPassword" maxlength="32" name="confirmPassword">
                                <div class="required-icon">*</div>
                            </div>
                            <div class="input-group w400 mt10 userInfo">
                                <div class="input-group-addon">手机号码</div>
                                <input type="text" class="form-control" id="mobile" maxlength="11" name="mobile">
                                <div class="required-icon">*</div>
                            </div>
                            <div class="input-group w400 mt10 userInfo">
                                <div class="input-group-addon">电子邮箱</div>
                                <input type="text" class="form-control" id="email" maxlength="32" name="email">
                                <div class="required-icon">*</div>
                            </div>
                            <div class="input-group w400 mt10 userInfo">
                                <div class="input-group-addon">所属组织</div>
                                <input type="text" class="form-control" id="orgName" maxlength="32" name="orgName" disabled>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="submintModify">保存</button>
                    <button type="button" class="btn btn-default hideModal" data-dismiss="modal">取消</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript" src="../../js/commonJs.js"></script>
<!--<script type="text/javascript" src="../../js/validateform.js"></script>-->

<script type="text/javascript">
    $(function () {
        var type = getQueryString('type');
        $('#modal2').modal({backdrop: 'static', keyboard: false});
        $('.hideModal').on('click', function () {
            $('.modalDom', window.parent.document).hide();
        });
        var user = window.sessionStorage.getItem('managerUser');
        var loginUser = null;
        $.ajax({
            type: 'POST',
            url: ctx + "/user/query",
            dataType: 'json',
            data: {
                loginCode: user
            },
            success: function(data){
                loginUser = data.data.rows[0];
                $('#loginCode').val(loginUser.loginCode);
                $('#userName').val(loginUser.userName);
                $('#mobile').val(loginUser.mobile);
                $('#email').val(loginUser.email);
                $('#orgName').val(loginUser.orgName);
                if(type=='pwd'){
                    $('#userName+.required-icon').hide();
                    $('.pwd').show();
                    $('.userInfo').hide();
                    $('#userName').attr('disabled','disabled');
                }else if(type=='user'){
                    $('.pwd').hide();
                    $('.userInfo').show();
                    $('#userName').removeAttr('disabled');
                }
                $('.modalDom', window.parent.document).show();
            }
        });

        $('#submintModify').click(function () {
            if(type=='pwd'){
                var confirmPassword = $("#confirmPassword").val();
                var newPassword = $("#newPassword").val();
                if(confirmPassword==newPassword&&confirmPassword.length>6){
                    $.ajax({
                        type: 'POST',
                        url: ctx + "/user/passwordModify",
                        dataType: 'json',
                        data: {
                            loginCode: $("#loginCode").val(),
                            oldPassword: $("#oldPassword").val(),
                            newPassword: newPassword
                        },
                        success: function(data){
                            if(data.code == 200){
                                $('.modalDom', window.parent.document).hide();
                            } else if(data.code==30005){
                                alert('原密码不正确');
                            }
                        }
                    });
                }else if(confirmPassword!=newPassword){
                    alert('新密码与确认密码不一致');
                }else if(confirmPassword.length<=6){
                    alert('新密码请大于6位');
                }
            }else if(type=='user'){
                var userName = $("#userName").val();
                var email = $("#email").val();
                var mobile = $("#mobile").val();
                var emailReg = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/;
                var mobileReg = /^1[3578][0-9]{9}$/;
                if(userName.trim()!=''&&emailReg.test(email)&&mobileReg.test(mobile)){
                    $.ajax({
                        type: 'POST',
                        url: ctx + "/user/updateUserInfo",
                        dataType: 'json',
                        data: {
                            loginCode: $("#loginCode").val(),
                            userName: userName,
                            email: email,
                            mobile: mobile
                        },
                        success: function(data){
                            if(data.code == 200){
                                $('.modalDom', window.parent.document).hide();
                            }
                        }
                    });
                }else if(userName.trim()==''){
                    alert("用户名不能为空");
                }else if(!emailReg.test(email)) {
                    alert('请正确填写Email');
                }else if(!mobileReg.test(mobile)){
                    alert('请正确填写手机号码');
                }
            }
        });

    });

</script>
</body>
</html>